// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import axios from "axios"
import VueQuillEditor from 'vue-quill-editor'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import 'quill/dist/quill.core.css' // import styles
import 'quill/dist/quill.snow.css' // for snow theme
import 'quill/dist/quill.bubble.css' // for bubble theme

Vue.use(VueQuillEditor)
Vue.use(ElementUI)

Vue.config.productionTip = false
Vue.prototype.$axios = axios
axios.defaults.baseURL = '/api'
// http request 拦截器
// axios.interceptors.request.use(
//   config => {
//     console.log("config", config)
//     let whiteList = ['/user/login', '/user/regist'];
//     if (whiteList.indexOf(config.url) == -1) {
//       const userInfo = JSON.parse(localStorage.getItem('blogUserInfo'));
//       if (userInfo && userInfo.user_id > 0) { // 判断是否存在token，如果存在的话，则每个http header都加上token
//         // config.headers.authorization = token  //请求头加上token
//       } else {
//         router.push("/login");
//       }
//     }
//     return config
//   },
//   err => {
//     router.push("/login");
//     return Promise.reject(err);
//   }
// );

// 全局路由守卫
router.beforeEach((to, from, next) => {
  let whiteList = ['/', '/home', '/login', '/register'];
  // console.log(123, to, to.path.indexOf(whiteList))
  if (whiteList.indexOf(to.path) == -1) {
    const userInfo = JSON.parse(localStorage.getItem('blogUserInfo'));
    if (userInfo && userInfo.user_id > 0) { // 判断是否存在token，如果存在的话，则每个http header都加上token
      console.log("登录成功", userInfo);
      next();
    } else {
      alert("请登录");
      next('/login');
    }
  } else {
    next();
  }
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})